import { createWebHistory,createRouter} from "vue-router";
const product_prefix = "productManage";
const order_prefix = "orderManage";
const user_prefix = "userManage";
const file_prefix= "/index.vue";
const modules = import.meta.glob("../views/**/**.vue");
const modulesLs ={
    [`../views/${product_prefix}/index.vue`]: () => import("../views/productManage/index.vue"),
    [`../views/${order_prefix}/index.vue`]: () => import("../views/orderManage/index.vue"),
    [`../views/${user_prefix}/index.vue`]: () => import("../views/userManage/index.vue"),
    [`../views/${product_prefix}/productDetail/index.vue`]:()=>import("../views/productManage/productDetail/index.vue")
}// 2.动态导入的时候直接，引用
const subRouter = [
    {
        path:"product-manage",
        name:"productManage",
        component:modulesLs[`../views/productManage/index.vue`],
        meta:{
            title:"商品管理",
            keepAlive:true
        },
    },
    {
        path:"order-manage",
        name:"orderManage",
        component:modulesLs[`../views/${order_prefix}/index.vue`],
        meta:{
            title:"订单管理",
            keepAlive:false
        }
    },
    {
        path:"user-manage",
        name:"userManage",
        component:modules['../views/' + user_prefix + file_prefix],
        meta:{
            title:"用户管理",
            keepAlive:false
        }
    },
    
]
const routes = [
        {
            path:"/",
            redirect:"/nav",
        },
        {
            path:"/nav",
            name:"Nav",
            component:()=>import("../views/Nav.vue"),
            meta:{
                keepAlive:true
            }
        },
        {
            path:"/test",
            name:"Test",
            component:()=>import("../views/Test.vue"),
            meta:{
                keepAlive:true
            }
        },
        {
            path:"/login",
            name:"Login",
            component:()=>import("../views/Login.vue"),
        },
        {
            path:"/reg",
            name:"Reg",
            component:()=>import("../views/Reg.vue"),
        },
        {
            path:"/index",
            name:"Index",
            component:()=>import("../views/Index.vue"),
            meta:{
                active:true,
                keepAlive:true
            },
           
            children:subRouter
        },
        {
            path:"/product-detail/:id",
            name:"productDetail",
            component:modulesLs[`../views/${product_prefix}/productDetail/index.vue`],
            meta:{
                title:"产品详情"
            },
        },
]
const router = createRouter({
    history:createWebHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    //我在这里模仿了一个获取用户信息的方法
 let isLogin = Boolean(window.localStorage.getItem('isLogin'));
    if (isLogin) {
        next()
    } else {
        if(to.path === '/index'){
           alert("当前未登录，正在跳转登录界面")
           next("/login")
       }else if(to.path === '/nav' || to.path==='/reg' || to.path === '/login'){
        next()
       }else{
        next("/")
       }
        
   } 
})

export {subRouter,router}